import 'package:flutter/cupertino.dart';

/// id : "c1"
/// title : "意大利"
/// color : "9C27B0"

class CategoryModel {
  String _id;
  String _title;
  String _color;

  // 将String直接转换为Colors类型，用于背景颜色
  Color _cColor;

  String get id => _id;

  String get title => _title;

  String get color => _color;

  Color get cColor => _cColor;

  CategoryModel({String id, String title, String color}) {
    _id = id;
    _title = title;
    _color = color;
  }

  CategoryModel.fromJson(dynamic json) {
    _id = json["id"];
    _title = json["title"];
    _color = json["color"];
    // 将String指定转换为16进制的int
    int colorInt = int.parse(_color, radix: 16);
    // 将int类型的color值补充透明度并转换为Color类型
    _cColor = Color(colorInt | 0xFF000000);
  }

  Map<String, dynamic> toJson() {
    var map = <String, dynamic>{};
    map["id"] = _id;
    map["title"] = _title;
    map["color"] = _color;
    return map;
  }

  @override
  String toString() {
    return 'CategoryModel{_id: $_id, _title: $_title, _color: $_color}';
  }
}
